package de.reondo.jos;

import org.jgrapht.Graph;
import org.jgrapht.graph.DefaultDirectedWeightedGraph;

public class PackageAnalyser {

	/**
	 * Creates a package graph based on a given class graph.
	 * @param graph
	 * @return
	 */
	public static Graph<Package, PackageDependencyEdge> make(Graph<Class<?>, ClassDependencyEdge> classGraph) {
		Graph<Package, PackageDependencyEdge> graph = new DefaultDirectedWeightedGraph<Package, PackageDependencyEdge>(PackageDependencyEdge.class);
	
		for (ClassDependencyEdge edge : classGraph.edgeSet()) {
			Package sourcePkg = classGraph.getEdgeSource(edge).getPackage();
			Package targetPkg = classGraph.getEdgeTarget(edge).getPackage();
			graph.addVertex(sourcePkg);
			graph.addVertex(targetPkg);
			graph.addEdge(sourcePkg, targetPkg, new PackageDependencyEdge(PackageDependencyType.USES, sourcePkg, targetPkg));
		}
		
		return graph;
	}
	
}
